﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Utils;
using Utils.FileFolderModule;
using Utils.JsonModule;

public class KnightController : MonoBehaviour
{
    public int level = 0;

    public int hp = 0;
    public int mp = 0;
    public int arm = 0;

    public TextMeshProUGUI hpt;
    public TextMeshProUGUI mpt;
    public TextMeshProUGUI armt;
    public TextMeshProUGUI price;
    public TextMeshProUGUI gemt;
    
    private void Awake()
    {
        hpt = UITool.GetComponent<TextMeshProUGUI>("HP/Value", gameObject);
        mpt = UITool.GetComponent<TextMeshProUGUI>("MP/Value", gameObject);
        armt = UITool.GetComponent<TextMeshProUGUI>("Arm/Value", gameObject);
        price = UITool.GetComponent<TextMeshProUGUI>("ConfirmBtn/Value", gameObject);
        gemt = UITool.GetComponent<TextMeshProUGUI>("Gem/Value", gameObject.transform.parent.gameObject);
        gemt.text = GameController.Instance().playerDataCtr.playerData.gemNum.ToString();
        UITool.GetComponent<Button>("ConfirmBtn",gameObject).onClick.AddListener(Upgrade);
        try
        {
            Knight knight = JsonHandler.GetObjectFromJson<Knight>(FilePathConfigue.KnightData);
            level = knight.level;
            hp = knight.hp;
            mp = knight.mp;
            arm = knight.arm;

            hpt.text = hp.ToString();
            mpt.text = mp.ToString();
            armt.text = arm.ToString();
            price.text = GetCost().ToString();
            
        }
        catch (Exception e)
        {
            string knightJson = JsonHandler.GetJsonFromObject(new Knight());
            FileHandler.ReWriteFile(FilePathConfigue.KnightData, knightJson);
            Knight knight = JsonHandler.GetObjectFromJson<Knight>(FilePathConfigue.KnightData);
            level = knight.level;
            hp = knight.hp;
            mp = knight.mp;
            arm = knight.arm;

            hpt.text = hp.ToString();
            mpt.text = mp.ToString();
            armt.text = arm.ToString();
            price.text = GetCost().ToString();
        }
    }

    private int GetCost()
    {
        switch (level)
        {
            case 0:
                hp = 4;
                mp = 100;
                arm = 4;
                return 1000;
            case 1:
                hp = 6;
                mp = 120;
                arm = 5;
                return 2000;
            case 2:
                hp = 8;
                mp = 150;
                arm = 6;
                return 4000;
            case 3:
                hp = 10;
                mp = 190;
                arm = 7;
                return 8000;
            case 4:
                hp = 12;
                mp = 240;
                arm = 8;
                return 16000;
            case 5:
                hp = 14;
                mp = 300;
                arm = 9;
                return -1;
            default:
                return -1;
        }
    }

    private void Upgrade()
    {
        if (GetCost()!=-1  && GameController.Instance().playerDataCtr.playerData.gemNum > GetCost())
        {
            GameController.Instance().playerDataCtr.playerData.gemNum  -= GetCost();
            level++;
            price.text = GetCost().ToString();
            hpt.text = hp.ToString();
            mpt.text = mp.ToString();
            armt.text = arm.ToString();
            gemt.text = GameController.Instance().playerDataCtr.playerData.gemNum.ToString();
            Knight knight = new Knight
            {
                level = level,
                hp = hp,
                mp = mp,
                arm = arm
            };
            string knightJson = JsonHandler.GetJsonFromObject(knight);
            FileHandler.ReWriteFile(FilePathConfigue.KnightData, knightJson);
            string playerDataJson = JsonHandler.GetJsonFromObject<PlayerData>(GameController.Instance().playerDataCtr.playerData);
            FileHandler.ReWriteFile(FilePathConfigue.PlayerData, playerDataJson);
        }
        
    }
}